package com.motionlog.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.PriorityQueue;

import com.motionlog.R;
import com.motionlog.model.Base;
import com.motionlog.model.Database;
import com.motionlog.view.DiagramView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class StatisticDiagram extends Activity implements OnClickListener {

	private static final String TAG = null;
	Button selectMotion;
	Base curentMotion = null;
	DiagramView mDiagramView;
	Database mDb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_diagram);

		mDb = Database.getInstance(this);

		selectMotion = (Button) findViewById(R.id.select_motion);
		selectMotion.setOnClickListener(this);
		mDiagramView = (DiagramView) findViewById(R.id.diagram_view);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.select_motion:
			Intent i = new Intent(StatisticDiagram.this, SelectMotion.class);
			i.putExtra("activity", StatisticDiagram.class.getName());
			startActivityForResult(i, 0);
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		android.util.Log.d(TAG, "motion.id: " + motion.id);
		if (motion.id != -1) {
			selectMotion.setText(motion.name);
			curentMotion = motion;

			int week1 = mDb.getMotionLogCount(motion.id, 1);
			int week2 = mDb.getMotionLogCount(motion.id, 2) - week1;
			int week3 = mDb.getMotionLogCount(motion.id, 3) - week1 + week2;
			int week4 = mDb.getMotionLogCount(motion.id, 4) - week1 + week2
					+ week3;
			android.util.Log.d(TAG, "week1: " + week1 + ", week2: " + week2
					+ ", week3: " + week3 + ", week4: " + week4);

			ArrayList<Integer> array = new ArrayList<Integer>();
			array.add(week1);
			array.add(week2);
			array.add(week3);
			array.add(week4);

			int max = Collections.max(array);

			mDiagramView.setLine(max);
			LinkedHashMap<String, Integer> datas = new LinkedHashMap<String, Integer>();
			datas.put("这一周", week1);
			datas.put("两周前", week2);
			datas.put("这三周", week3);
			datas.put("这四周", week4);
			mDiagramView.setData(datas);
			mDiagramView.invalidate();
		}
	}

	public static Base motion = new Base();

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Nested tabactivities doesn't work with startActivityForResult
		android.util.Log.d(TAG, "diagram resutl");
	}
}
